package com.fy.fyzf.glideUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseApp;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.DensityUtil;
import com.fy.fyzf.weight.GlideCircleWithBorder;
import com.fy.fyzf.weight.RoundedCornersTransform;

import java.io.ByteArrayOutputStream;


/**
 * Created by  on 2017/11/13.
 * 加载图片工具类
 * Glide
 * 1.
 * load(String string)	string可以为一个文件路径、uri或者url
 * load(Uri uri)	uri类型
 * load(File file)	文件
 * load(Integer resourceId)	资源Id,R.drawable.xxx或者R.mipmap.xxx
 * load(byte[] model)
 */

public class GlideUtils {

    public static void loadImage(String url, ImageView image) {
        Glide.with(image.getContext())
                .load(url)
                .into(image);
    }

    public static void loadBordoerImg(String url, ImageView image,Context context){
        //带白色边框的圆形图片
        Glide.with(context).load(url)
                .apply(new RequestOptions().error(context.getResources().getDrawable(R.mipmap.head_nodata))
                        .centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL)
                        .transform(new GlideCircleWithBorder(context,2,Color.parseColor("#DF2627")))
                ).into(image);
    }

    private static void loadImage(String url, ImageView image, RequestOptions requestOptions) {
        Glide.with(image.getContext())
                .load(url)
                .apply(requestOptions)
                .error(R.mipmap.icon_no_data1)
                .into(image);
    }
    private static void loadImage(byte[] bytes, ImageView image, RequestOptions requestOptions) {
        Glide.with(image.getContext())
                .asBitmap()
                .load(bytes)
                .apply(requestOptions)
                .into(image);
    }
    public static void loadCircleImage(String url, ImageView image) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.circleCrop();
        Glide.with(image.getContext())
                .load(url)
                .apply(requestOptions)
                .error(R.mipmap.head_nodata)
                .into(image);
    }

    @SuppressLint("CheckResult")
    public static void loadRoundedCornersImage(String url, ImageView image, float radius) {

        RoundedCornersTransform transform = new RoundedCornersTransform(BaseApp.getContext(),radius);
        transform.setNeedCorner(true, true, true, true);
        RequestOptions options = new RequestOptions().transform(transform);
        Glide.with(BaseApp.getContext()).asBitmap().load(url).apply(options).error(BaseApp.getContext().getResources().getDrawable(R.mipmap.nodata_common)).into(image);
    }

    @SuppressLint("CheckResult")
    public static void loadRoundedCornersImage_Local(int img, ImageView image, float radius) {

        RoundedCornersTransform transform = new RoundedCornersTransform(BaseApp.getContext(),radius);
        transform.setNeedCorner(true, true, true, true);
        RequestOptions options = new RequestOptions().transform(transform);
        Glide.with(BaseApp.getContext()).asBitmap().load(img).apply(options).error(BaseApp.getContext().getResources().getDrawable(R.mipmap.nodata_common)).into(image);
    }
    public static void loadRoundedCornersImage(String url, ImageView image, float radius,
                                               GlideRoundedCornersTransform.CornerType cornerType) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.optionalTransform(new GlideRoundedCornersTransform((int) (dp2px
                (radius) + 0.5f),
                cornerType));
        loadImage(url,image,requestOptions);
    }

    public static void loadRoundedCornersImage(Bitmap bm, ImageView image, float radius,
                                               GlideRoundedCornersTransform.CornerType cornerType) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.optionalTransform(new GlideRoundedCornersTransform((int) (dp2px
                (radius) + 0.5f),
                cornerType));
        loadImage(Bitmap2Bytes(bm),image,requestOptions);
    }
    /**
     * 把Bitmap转Byte
     */
    public static byte[] Bitmap2Bytes(Bitmap bm){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }
    public static float dp2px(float value) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getDisplayMetrics());
    }
    public static DisplayMetrics getDisplayMetrics() {
        return Resources.getSystem().getDisplayMetrics();
    }

    public static void loadSingleRadius(Context context,int radius,String url,ImageView imageView){
        RoundedCornersTransform transform = new RoundedCornersTransform(context,radius);
        transform.setNeedCorner(true, true, false, false);
        RequestOptions options = new RequestOptions().transform(transform);
        Glide.with(context).asBitmap().load(url).apply(options).error(context.getResources().getDrawable(R.mipmap.nodata_common)).into(imageView);
    }
}
